package com.tonglin.educms.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tonglin.commonutils.R;
import com.tonglin.educms.entity.CrmBanner;
import com.tonglin.educms.service.CrmBannerService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author tonglin
 * @since 2021-05-13
 */
@RestController
@RequestMapping("/educms/banneradmin")
@CrossOrigin
public class BannerAdminController {
    @Autowired
    CrmBannerService crmBannerService;

    @ApiOperation(value = "分页查询banner")
    @GetMapping("pageBanner/{current}/{limit}")
    public R pageBanner(@PathVariable("current") long current, @PathVariable("limit") long limit){

        Page<CrmBanner> pageBanner = new Page<>(current,limit);

        crmBannerService.page(pageBanner,null);

        long total = pageBanner.getTotal();
        List<CrmBanner> bannerList = pageBanner.getRecords();

        return R.ok().data("total",total).data("items",bannerList);
    }

    //添加banner
    @ApiOperation(value = "添加banner")
    @PostMapping("addBanner")
    public R addBanner(@RequestBody CrmBanner crmBanner){
        crmBannerService.save(crmBanner);
        return R.ok();
    }

    //修改banner
    @ApiOperation(value = "修改banner")
    @PutMapping("update")
    public R updateById(@RequestBody CrmBanner banner){
        crmBannerService.updateById(banner);
        return R.ok();
    }
    @ApiOperation(value = "删除banner")
    @DeleteMapping("remove/{id}")
    public R remove(@PathVariable String id){
        crmBannerService.removeById(id);
        return R.ok();
    }
}

